/*************************************************************************
	> File Name: globalmsg.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年01月25日 星期四 09时58分18秒
 ************************************************************************/

#include<stdio.h>
#include "globalmsg.h"

static U8 g_iMsgData[DAS_MAX] = {0};
static void *  g_pTask[MAX_TASK_DIVID][DAS_TASK_MAX];
static int g_iTotal = 1;
static int m_icheck = 0;
static const char  g_sError[DAS_RESULT_MAX][30] ={
    "SUCCESS",
    "INVALID",
    "TIMEOUT",
    "PASSWD NOT PASS",
    "COLLISION",
    "FILE NOT FAILD",
    "FILE OPEN FAILD",
    "JSON PARSE FAILD",
    "JSON FIELD NOT EXIST",
    "MEMORY NOT ALLOC",
    "GATEWAY ERROR",
    "NO INFO",
    "DEVICE EXIST",
    "DEVICE NOT EXIST",
    "DATA FAILD",
    "INVALID ADDR",
    "PARAMS ERROR",
    "CONNECT FAILD",
    "CONFIGURE FAILD",
    "PROTOCOL ERROR",
    "DWONLOAD FAILED",
    "BEING RESTART",
    "FORCESET",
    "ADDR ERR",
    "NO ITEM",
    "ACQQUIRE FAILED",
    "PARSE FAILED"
};
/*
 * 错误
 */
const char *GetError(DAS_RESULT status)
{
    if (status < DAS_RESULT_MAX && status>=0)
        return g_sError[status];
    else
        return g_sError[0];
}

void InitMsg()
{
    for (int i = 0; i< DAS_MAX;i++) {
        g_iMsgData[(DAS_TYPE)i] = 0;
    }
    g_iMsgData[DAS_MSG_STATUS] |= 0xFF;
    m_icheck = 0;
}

void SetCenterNum(int num)
{
    g_iTotal = num;
}

S32 TotalCenter()
{
    return g_iTotal;
}

void SetMsgStatus(DAS_TYPE type, BOOL status,S32 index)
{
    if (index == -1)
        index = 0;
    if (status)
        g_iMsgData[type] |= 0x01<<index;
    else {
        g_iMsgData[type] &= ~(0x01<<index);
    }
    if (type == DAS_BUF_DOG && status == FALSE) {
        m_icheck = 0;
    }
}

S32  GetDogCheck() {
    return ++m_icheck;
}

BOOL GetMsgStatus(DAS_TYPE type, S32 index)
{
    if (index == -1)
        index = 0;
    return g_iMsgData[type]&(0x01<<index);
}

BOOL GetMsgStatus(int type, S32 index)
{
    if (index == -1)
        index = 0;
    return GetMsgStatus((DAS_TYPE)type,index);
}

void AddTask(DAS_TASK task,S32 index,void *pTask)
{
    g_pTask[index][task] = pTask;
}

void* GetTask(DAS_TASK task,S32 index)
{
    return g_pTask[index][task];
}


